//
//  ByViewLookDoctorSegment.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/11.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByViewLookDoctorSegment.h"

@implementation ByViewLookDoctorSegment
{
    UIButton    *_hospitalButton;
    UIButton    *_subjectButton;
    UIView      *_line;
    NSInteger   _preTag;
}
- (instancetype)init{
    if (self = [super init]) {
        [self createViews];
    }
    return self;
}

- (void)createViews{
    _hospitalButton = [[UIButton alloc] init];
    [self addSubview:_hospitalButton];
    CGFloat width = (SCREEN_WIDTH - 20) /2;
    [_hospitalButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    _hospitalButton.titleLabel.font = FONT(14);
    _hospitalButton.tag = 10;
    [_hospitalButton setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
    
    _subjectButton = [[UIButton alloc] init];
    [self addSubview:_subjectButton];
    [_subjectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_hospitalButton.mas_right);
        make.top.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    _subjectButton.titleLabel.font = FONT(14);
    _subjectButton.tag = 11;
    [_subjectButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    _line = [[UIView alloc] init];
    [self addSubview:_line];
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.bottom.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 2, 1));
    }];
    _line.backgroundColor = BY_THEME_COLOR;
    
    [_hospitalButton addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    [_subjectButton addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    _hospitalButton.selected = YES;
    _preTag = 10;
    
}

- (void)selectAction:(UIButton *)button{
    if (button.tag != _preTag) {
        [_line mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@((button.tag - 10) * (SCREEN_WIDTH / 2)));
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 2, 1));
            make.bottom.equalTo(@(0));
        }];
        UIButton *preButton = [self viewWithTag:_preTag];
        preButton.selected = NO;
        [preButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        
        button.selected = YES;
        [button setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
        _preTag = button.tag;
    }
    
    if (self.selectedSegmentBlock) {
        self.selectedSegmentBlock(button.tag);
    }
    
}

#pragma mark - 设置数据
- (void)setHospital:(NSString *)hospital{
    NSString *hospitalStr = [NSString stringWithFormat:@"分院筛选：%@",hospital];
    if (_preTag == _hospitalButton.tag) {
        [_hospitalButton setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
    }else{
        [_hospitalButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
    [_hospitalButton setTitle:hospitalStr forState:UIControlStateNormal];
}

- (void)setSubject:(NSString *)subject{
    NSString *subjectStr = [NSString stringWithFormat:@"科室筛选：%@",subject];
    if (_preTag == _subjectButton.tag) {
        [_subjectButton setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
    }else{
        [_subjectButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
    [_subjectButton setTitle:subjectStr forState:UIControlStateNormal];
}
@end
